//index.js
Page({
	data: {
		config: {},
    banners: [],
    categories:[],
    sysConfig:[],
    homeContent:[],
    mask: false,
	},

	onLoad: function (options) {
		this.options = options;
		this.onPullDownRefresh();
	},

	onShow: function (options) {
		console.log(this.options)
		this.tryAllowShareMessage(this.options);
	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {
		Promise.all([
			wx.$model.home.basicConfig().resolved(),
			wx.$model.home.bannerList().resolved(),
      wx.$model.home.categories().resolved(),
      wx.$model.home.sysConfig().resolved(),
      wx.$model.home.getHomeContent().resolved(),
      
		]).then((res) => {
			const data = {};
			if (res[0]) data.config = res[0];
			if (res[1]) data.banners = res[1];
      if (res[2]) data.categories = res[2].data;
      if (res[3]) data.sysConfig = res[3];
      if (res[4]) data.homeContent = res[4].data;
			this.setData(data, () => {
				wx.stopPullDownRefresh();
			});
		});
	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {
		const config = this.data.config;
		let title = '';
		let imageUrl = '';
		if (config) {
			if (!title) title = config.title;
			if (!imageUrl) imageUrl = config.imageUrl;
		}

		const query = "store_id=" + wx.$getStoreId();
		return {
			title: title,
			path: "pages/index/index?" + query,
			query: query,
			imageUrl: imageUrl
		};
	},

	/**
	 * 用户点击右上角分享朋友圈
	 */
	onShareTimeline: function () {
		return this.onShareAppMessage();
	},

	/**
	 * 打电话
	 */
	callPhone: function () {
		wx.makePhoneCall({
			phoneNumber: this.data.config.doctor.phone
		})
	},

	/**
	 * 跳转地址
	 */
	gotoLocation: function() {
		let location = this.data.sysConfig.hospital_location;
		location = location.split(',', 2).map(parseFloat);
		const address = this.data.sysConfig.hospital_addr;

		wx.openLocation({
			address: address,
			latitude: location[1],
			longitude: location[0]
		});
  },
  
	/**
	 * 复制微信账号
	 */
	copyWechat: function () {
		wx.setClipboardData({
			data: this.data.config.doctor.wechat,
			success(res) {
				// wx.hintSuccess('分诊师微信号已复制，打开微信粘贴添加√');
				wx.showToast({
					title: '分诊师微信号已复制，打开微信粘贴添加√',
					icon: 'none'
				})
			}
		})
  },
  
  goReservation () {
    wx.switchTab({
      url: '/pages/yuyue/index',
    })
  },
  goMap () {
    wx.switchTab({
      url: '/pages/map/index',
    })
  },
  goParse (e) {
    let key = e.currentTarget.dataset.key
    let name = e.currentTarget.dataset.name
    wx.navigateTo({
      url: '/pages/index/parse/index?key=' + key+'&name='+name,
    })
  },
  golist (e) {
    let key = e.currentTarget.dataset.key
    wx.navigateTo({
      url: '/pages/index/list/index?key=' + key,
    })
  },

  maskOpen () {
    this.setData({
      mask: true
    })
  },
  maskClose () {
    this.setData({
      mask: false
    })
  }
})